package com.example.video.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServiceException.class)
    @ResponseBody
    public Object customHandler(ServiceException e) {
        return ResultModel.error(e.getCode(), e.getMessage(), e.getContent());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object exceptionHandler(Exception e) {
        log.error("unknown error", e);
        return ResultModel.error(ResultStatus.SYSTEM_ERR);
    }

    /**
     * 登录认证异常
     */
    @ExceptionHandler({UnauthenticatedException.class, AuthenticationException.class})
    @ResponseBody
    public Object authenticationException(Exception e) {
        return ResultModel.error(ResultStatus.ADMIN_AUTHC_ERR);
    }

    /**
     * 权限异常
     */
    @ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
    @ResponseBody
    public Object authorizationException(Exception e) {
        return ResultModel.error(ResultStatus.ADMIN_AUTHZ_ERR);
    }


    /**
     * 没有访问权限异常处理类
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Object httpRequestMethodNotSupportedExceptionHandler(Exception e) {
        e.printStackTrace();
        return ResultModel.error(ResultStatus.ADMIN_AUTHC_ERR);
    }


}
